﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DockSample
{
    public partial class InformationFlowModelForm : DummyModelForm
    {
        public InformationFlowModelForm()
        {
            InitializeComponent();
            initContextMenuStrip();
            //SystemVar.mainForm.modelFormList.Add(this);
            SystemVar.mainForm.SolutionExplorer.updateSolutionExplorerTreeView();
        }
        /************************************************************************/
        /* 传个hasMainForm=true，实现把当前form加入mainform的modelFormList，张帆改写的构造函数                                                                     */
        /************************************************************************/
        public InformationFlowModelForm( bool hasMainForm)
        {
            if (hasMainForm)
            {
                SystemVar.mainForm.modelFormList.Add(this);
            }
        }

        protected override void DummyModelForm_Click(object sender, EventArgs e)
        {
            SystemVar.setSelectableToolBox(this);

            //base.DummyModelForm_Click(sender, e);
        }
        //
        protected override void DummyModelForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            base.DummyModelForm_FormClosing(sender, e);
        }

        protected override void DummyModelForm_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            #region 左键双击
            if (e.Clicks == 2 && e.Button == MouseButtons.Left)
            {
                foreach (OriginalDiagram diag in diagramList)
                {
                    if (diag is InformationSourceDiagram)
                    {
                        InformationSourceDiagram obj = (InformationSourceDiagram)diag;
                        Rectangle rect = new Rectangle(diag.UpLeftpoint.X, diag.UpLeftpoint.Y, diag.Width, diag.Height);
                        Point pt = new Point(e.X, e.Y);
                        if (rect.Contains(pt))
                        {
                            if (!obj.HasModelForm)
                            {
                                InformationFlowModelForm modelForm = SystemVar.mainForm.createNewInforFlowModelForm("InformationFlowModelForm/ " + obj.Name);
                                
                                obj.NewModelForm = modelForm;
                                obj.HasModelForm = true;

                               

                                //SystemVar.mainForm.modelFormList.Add(this);
                                addExternalInterface(obj, modelForm);
                                SystemVar.mainForm.SolutionExplorer.updateSolutionExplorerTreeView();
                                SystemVar.mainForm.modelFormList.Add(modelForm);
                            }
                            else
                            {
                                obj.NewModelForm.Focus();
                            }
                        }
                    }
                }
            }
            #endregion

            #region 左键单击
            else 
            {

                ClearSelection();
                Point pt = new Point(e.X, e.Y);
                //添加图形对象
                if (SystemVar.currentDiagramFlag == Diagram.Process || SystemVar.currentDiagramFlag == Diagram.InformationSource
                    || SystemVar.currentDiagramFlag == Diagram.ExternalInterfaceIn || SystemVar.currentDiagramFlag == Diagram.ExternalInterfaceOut)
                {
                    AddDiagram(SystemVar.currentDiagramFlag, new Point(e.X, e.Y));

                }else if(SystemVar.currentDiagramFlag==Diagram.DataStore)
                {

                    AddDiagram(SystemVar.currentDiagramFlag, new Point(e.X, e.Y));


                }
                //画线准备
                else if (SystemVar.currentDiagramFlag == Diagram.InformationFlow)
                {
                    DrawingLinePre(pt);
                }

//                 else if (SystemVar.currentDiagramFlag == Diagram.SelfRoundLine)
//                 {
//                     DrawSelfRoundLine(pt);
//                 }
                //选中操作
                else if (SystemVar.currentDiagramFlag == Diagram.NullObject)
                {
                    SelectDiagram(pt);
                    SelecteLine(pt);
                }
                //缩放准备
                if (preSelectedDiagram != null)
                {
                    selectedMagnifyingDiagramPoint(pt);
                }
            }
            #endregion

            #region 右击
            if (e.Button == MouseButtons.Right)
            {
                ShowContextMenuStrip(new Point(e.X, e.Y));
                //parentMainForm.currentDiagramFlag = Diagram.NullObject;
                //Cursor = System.Windows.Forms.Cursors.Arrow;
            }
            #endregion

            base.DummyModelForm_MouseDown(sender, e);
        }

        protected override void DummyModelForm_MouseEnter(object sender, EventArgs e)
        {
            switch (SystemVar.currentDiagramFlag)
            {
                case Diagram.Process:
                    Cursor = System.Windows.Forms.Cursors.Cross;
                    break;
                case Diagram.InformationSource:
                    Cursor = System.Windows.Forms.Cursors.Cross;
                    break;
                case Diagram.ExternalInterfaceIn:
                    Cursor = System.Windows.Forms.Cursors.Cross;
                    break;
                case Diagram.ExternalInterfaceOut:
                    Cursor = System.Windows.Forms.Cursors.Cross;
                    break;
                case Diagram.DataStore:
                    Cursor = System.Windows.Forms.Cursors.Cross;
                    break;
                case Diagram.InformationFlow:
                    Cursor = System.Windows.Forms.Cursors.UpArrow;
                    break;
                case Diagram.ComplexLine:
                    Cursor = System.Windows.Forms.Cursors.UpArrow;
                    break;
                case Diagram.SelfRoundLine:
                    Cursor = System.Windows.Forms.Cursors.UpArrow;
                    break;
                case Diagram.NullObject:
                    Cursor = System.Windows.Forms.Cursors.Default;
                    break;
            }
        }

        protected override void DummyModelForm_MouseMove(object sender, MouseEventArgs e)
        {
            Point pt = new Point(e.X, e.Y);
            //移动图形
            if (selectedDiagram != null && movingDiagramFlag)
            {
                MovingDiagram(selectedDiagram, pt);
            }
            //缩放图形
            else if (preSelectedDiagram != null && magnifyingDiagramFlag)
            {
                magnifyingDiagram(pt);
            }
            //画线
            else if (lineStartDiagram != null && drawingLineFlag)
            {
                DrawingLine(pt);
            }
        }

        protected override void DummyModelForm_MouseUp(object sender, MouseEventArgs e)
        {
            Point pt = new Point(e.X, e.Y);
            //移动图形
            if (selectedDiagram != null && movingDiagramFlag)
            {
                MovedDiagram(selectedDiagram, pt);
            }
            //缩放图形
            else if (preSelectedDiagram != null && magnifyingDiagramFlag)
            {
                magnifiedDiagram(pt);
            }
            //画线
            else if (lineStartDiagram != null && drawingLineFlag)
            {
                DrawedLine(SystemVar.currentDiagramFlag, pt);
            }
            base.DummyModelForm_MouseUp(sender, e);
        }

        private void initContextMenuStrip()
        {
            modelFormContextMenuStrip.Items[0].Visible = false;
            modelFormContextMenuStrip.Items[1].Visible = false;
            modelFormContextMenuStrip.Items[2].Visible = false;
            modelFormContextMenuStrip.Items[3].Visible = false;
            modelFormContextMenuStrip.Items[7].Visible = false;
            modelFormContextMenuStrip.Items[8].Visible = false;
            modelFormContextMenuStrip.Items[9].Visible = false;

            diagramMenuStrip.Items[0].Visible = false;
            diagramMenuStrip.Items[1].Visible = false;
            diagramMenuStrip.Items[2].Visible = false;
            diagramMenuStrip.Items[3].Visible = false;
            diagramMenuStrip.Items[5].Visible = false;
        }
    }
}
